package org.jeecg.modules.door.service.impl;

import org.jeecg.modules.door.entity.LwgyDoorGroupright;
import org.jeecg.modules.door.entity.LwgyJointDoor;
import org.jeecg.modules.door.entity.LwgyDoorGrouppassword;
import org.jeecg.modules.door.entity.LwgyDoorGroupcard;
import org.jeecg.modules.door.mapper.LwgyJointDoorMapper;
import org.jeecg.modules.door.mapper.LwgyDoorGrouppasswordMapper;
import org.jeecg.modules.door.mapper.LwgyDoorGroupcardMapper;
import org.jeecg.modules.door.mapper.LwgyDoorGrouprightMapper;
import org.jeecg.modules.door.service.ILwgyDoorGrouprightService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 分组管理
 * @Author: jeecg-boot
 * @Date:   2023-08-14
 * @Version: V1.0
 */
@Service
public class LwgyDoorGrouprightServiceImpl extends ServiceImpl<LwgyDoorGrouprightMapper, LwgyDoorGroupright> implements ILwgyDoorGrouprightService {

	@Autowired
	private LwgyDoorGrouprightMapper lwgyDoorGrouprightMapper;
	@Autowired
	private LwgyJointDoorMapper lwgyJointDoorMapper;
	@Autowired
	private LwgyDoorGrouppasswordMapper lwgyDoorGrouppasswordMapper;
	@Autowired
	private LwgyDoorGroupcardMapper lwgyDoorGroupcardMapper;
	
	@Override
	@Transactional
	public void saveMain(LwgyDoorGroupright lwgyDoorGroupright, List<LwgyJointDoor> lwgyJointDoorList,List<LwgyDoorGrouppassword> lwgyDoorGrouppasswordList,List<LwgyDoorGroupcard> lwgyDoorGroupcardList) {
		lwgyDoorGrouprightMapper.insert(lwgyDoorGroupright);
		if(lwgyJointDoorList!=null && lwgyJointDoorList.size()>0) {
			for(LwgyJointDoor entity:lwgyJointDoorList) {
				//外键设置
				entity.setGroupId(lwgyDoorGroupright.getId());
				lwgyJointDoorMapper.insert(entity);
			}
		}
		if(lwgyDoorGrouppasswordList!=null && lwgyDoorGrouppasswordList.size()>0) {
			for(LwgyDoorGrouppassword entity:lwgyDoorGrouppasswordList) {
				//外键设置
				entity.setGroupId(lwgyDoorGroupright.getId());
				lwgyDoorGrouppasswordMapper.insert(entity);
			}
		}
		if(lwgyDoorGroupcardList!=null && lwgyDoorGroupcardList.size()>0) {
			for(LwgyDoorGroupcard entity:lwgyDoorGroupcardList) {
				//外键设置
				entity.setGroupId(lwgyDoorGroupright.getId());
				lwgyDoorGroupcardMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(LwgyDoorGroupright lwgyDoorGroupright,List<LwgyJointDoor> lwgyJointDoorList,List<LwgyDoorGrouppassword> lwgyDoorGrouppasswordList,List<LwgyDoorGroupcard> lwgyDoorGroupcardList) {
		lwgyDoorGrouprightMapper.updateById(lwgyDoorGroupright);
		
		//1.先删除子表数据
		lwgyJointDoorMapper.deleteByMainId(lwgyDoorGroupright.getId());
		lwgyDoorGrouppasswordMapper.deleteByMainId(lwgyDoorGroupright.getId());
		lwgyDoorGroupcardMapper.deleteByMainId(lwgyDoorGroupright.getId());
		
		//2.子表数据重新插入
		if(lwgyJointDoorList!=null && lwgyJointDoorList.size()>0) {
			for(LwgyJointDoor entity:lwgyJointDoorList) {
				//外键设置
				entity.setGroupId(lwgyDoorGroupright.getId());
				lwgyJointDoorMapper.insert(entity);
			}
		}
		if(lwgyDoorGrouppasswordList!=null && lwgyDoorGrouppasswordList.size()>0) {
			for(LwgyDoorGrouppassword entity:lwgyDoorGrouppasswordList) {
				//外键设置
				entity.setGroupId(lwgyDoorGroupright.getId());
				lwgyDoorGrouppasswordMapper.insert(entity);
			}
		}
		if(lwgyDoorGroupcardList!=null && lwgyDoorGroupcardList.size()>0) {
			for(LwgyDoorGroupcard entity:lwgyDoorGroupcardList) {
				//外键设置
				entity.setGroupId(lwgyDoorGroupright.getId());
				lwgyDoorGroupcardMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		lwgyJointDoorMapper.deleteByMainId(id);
		lwgyDoorGrouppasswordMapper.deleteByMainId(id);
		lwgyDoorGroupcardMapper.deleteByMainId(id);
		lwgyDoorGrouprightMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			lwgyJointDoorMapper.deleteByMainId(id.toString());
			lwgyDoorGrouppasswordMapper.deleteByMainId(id.toString());
			lwgyDoorGroupcardMapper.deleteByMainId(id.toString());
			lwgyDoorGrouprightMapper.deleteById(id);
		}
	}
	
}
